package cn.befory.common;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.experimental.Accessors;

@Getter
@Accessors(chain = true)
@ApiModel(value = "响应对象")
public class Result<T> {

    @ApiModelProperty(value = "响应码",name = "code",dataType = "string")
    private String code;
    @ApiModelProperty(value = "响应说明",name = "msg",dataType = "string")
    private String msg;
    @ApiModelProperty(value = "响应数据",name = "data",dataType = "object")
    private T data;

    private Result(String code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static <T> Result<T> ok(T data){
        return new Result<T>(ExCode.OK.getCode(),ExCode.OK.getMsg(),data);
    }

    public static <T> Result<T> error(ExCode exCode){
        return new Result<T>(exCode.getCode(),exCode.getMsg(),null);
    }

    public static <T> Result<T> error(String msg){
        return new Result<T>(ExCode.E_90002.getCode(),msg,null);
    }

}
